﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ZaxLib.Collections;

namespace ZaxLib.Web.UI.WebControls
{
	[
	TypeConverter(typeof(ExpandableObjectConverter))
	]
	public class GridItem : WebControl, INamingContainer, IDataItemContainer
	{
		[
		Bindable(true),
		Category("Behavior"),
		DefaultValue(""),
		NotifyParentProperty(true)
		]
		public string Label
		{
			get
			{
				String s = (String)ViewState["Label"];
				return ((s == null) ? Field : s);
			}

			set
			{
				ViewState["Label"] = value;
			}
		}
		
		[
		Bindable(true),
		Category("Behavior"),
		DefaultValue(""),
		NotifyParentProperty(true)
		]
		public String Field
		{
			get
			{
				String s = ViewState["Field"] as String;
				return (s == null ? String.Empty : s);
			}
			set
			{
				ViewState["Field"] = value;
			}
		}

		protected override void RenderContents(HtmlTextWriter writer)
		{
			Dictionary<String, Object> data = DataItem as Dictionary<String, Object>;
			writer.Write(data[Field].ToString());
		}

		private Object _current;
		public GridItem(Object currentItem)
		{
			_current = currentItem;
		}

		public GridItem()
			: this(String.Empty)
		{
		}

		public Object DataItem
		{
			get
			{
				return _current;
			}
		}

		public int DataItemIndex
		{
			get
			{
				return 0;
			}
		}

		public int DisplayIndex
		{
			get
			{
				return DataItemIndex + 1;
			}
		}
	}
}
